#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <CUnit/Basic.h>
#include "sort.h"

#define N 10

void getRandomIntArray(int* array, int size) {
	int i;
	
	srand(time(NULL));
	for (i=0; i < size; ++i) {
		array[i] = rand()%10;
	}
}

void testSort(void(*sort)(int*, int)) {
	int i;
	int array[N] = {0};
	getRandomIntArray(array, N);
	sort(array, N);

	for (i=0; i < N-1; ++i) {
		CU_ASSERT(array[i] <= array[i+1]);
	}
}

void testQuickSort() {
	testSort(quicksort);
	CU_PASS("Testing quicksort passed");
}

void testBubbleSort() {
	testSort(bubblesort);
	CU_PASS("Testing bubblesort passed");
}

int main() {
	int nFailures = 0;
	CU_Suite* suite;

	CU_initialize_registry();
	suite = CU_add_suite("Sort algorithms", NULL, NULL);
	CU_add_test(suite, "Bubble sort test", testBubbleSort);
	CU_add_test(suite, "Quick sort test", testQuickSort);
	CU_basic_set_mode(CU_BRM_VERBOSE);
	CU_basic_run_tests();
	nFailures = CU_get_number_of_failures();
	printf("Run: suite [%d], successes [%d], failures [%d]\n", CU_get_number_of_suites_run(), CU_get_number_of_successes(), CU_get_number_of_failures());
	CU_cleanup_registry();

	return nFailures > 0 ? 1 : 0;
}

